<template>
  <div id="airports" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">基础数据</li>
        <li class="breadcrumb-item active" aria-current="page">机场信息</li>
      </ol>
    </nav>
    <template v-if="isSearchMode">
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="名称"
              size="12"
              v-model.trim="name0"
            />
          </div>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="三字代码"
              size="8"
              v-model.trim="threeCode0"
            />
          </div>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="国家代码"
              size="8"
              v-model.trim="countryCode0"
            />
          </div>
          <div class="row mb-2">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="10">10</option>
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="500">500</option>
            </select>
          </div>
          <div class="row mb-2">
            <button
              type="button"
              class="btn btn-primary mb-2"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              @click.stop="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-body">
        <button
          type="button"
          @click.stop="newUser()"
          class="btn btn-success"
          v-if="isAdmin"
        >
          新建
        </button>
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearch()"
        >
          查找
        </button>
      </div>
      <table class="table table-striped table-hover table-sm small">
        <thead>
          <tr>
            <th>机场代码</th>
            <th>城市代码</th>
            <th>城市/机场名</th>
            <th class="d-none d-md-table-cell">机场英文名</th>
            <th>国家代码</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="(info, index) in dataList"
            :key="info.id"
            @click.stop="edit(index)"
          >
            <td>{{ info.threeCode }}</td>
            <td>{{ info.cityCode }}</td>
            <td>{{ info.cityName }}<br />{{ info.airportName }}</td>
            <td class="d-none d-md-table-cell">
              {{ info.airportEnglishName }}
            </td>
            <td>{{ info.countryCode }}</td>
            <td><i class="mdi mdi-arrow-right fa-2"></i></td>
          </tr>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>

    <my-modal-airport ref="airportModal"></my-modal-airport>

  </div>
</template>

<script>
import $ from "jquery";
import { searchAirports, createAirport } from "@/api/basic-data.js";
import MyPagination from "@/components/my-pagination_h5.vue";
import MyModalAirport from "@/components/modal/my-modal-airport.vue";


export default {
  components: {
    MyPagination,
    MyModalAirport
  },
  data() {
    return {
      isSearchMode: false,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      threeCode0: "",
      countryCode0: "",
      name0: "",

      threeCode: "",
      countryCode: "",
      cityCode: "",
      cityName: "",
      nameCn: "",
      nameEn: "",

      dataList: [],
      email: "",
      name: "",
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
    readonly() {
      return this.isAdmin === false;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showSearch: function () {
      this.isSearchMode = true;
    },
    search: function () {
      this.isSearchMode = false;
      this.showLoading();
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        threeCode: this.threeCode0,
        countryCode: this.countryCode0,
        name: this.name0,
      };

      searchAirports(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    newUser: function () {
      this.threeCode = "";
      this.countryCode = "";
      this.cityCode = "";
      this.cityName = "";
      this.nameCn = "";
      this.nameEn = "";

      const params = {
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn,
      };

      this.$refs.airportModal
        .modal(params)
        .then((v) => {
          this.createUser(v);
        })
        .catch((ex) => {});
    },
    edit: function (index) {
      const info = this.dataList[index];
      this.threeCode = info.threeCode;
      this.countryCode = info.countryCode;
      this.cityCode = info.cityCode;
      this.cityName = info.cityName;
      this.nameCn = info.airportName;
      this.nameEn = info.airportEnglishName;

      const params = {
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn,
      };

      this.$refs.airportModal
        .modal(params)
        .then((v) => {
          this.createUser(v);
        })
        .catch((ex) => {});
    },
    createUser: function (v) {
      const params = {
        threeCode: v.threeCode,
        cityCode: v.cityCode,
        cityName: v.cityName,
        countryCode: v.countryCode,
        airportName: v.airportName,
        airportEnglishName: v.airportEnglishName,
      };

      createAirport(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.search();
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    reset: function () {
      this.threeCode0 = "";
      this.countryCode0 = "";
      this.name0 = "";
    },
  },
};
</script>
